package org.zjx.command.game;

import com.alibaba.fastjson2.JSON;
import org.zjx.command.Command;
import org.zjx.command.CommandFactory;
import org.zjx.command.basic.ClickCommand;
import org.zjx.command.basic.KeyCombinationCommand;
import org.zjx.command.composite.GameMoveLocationCommand;
import org.zjx.core.IWindow;
import org.zjx.entity.Address;
import org.zjx.entity.GameMouse;
import org.zjx.model.game.Npc;
import org.zjx.model.game.Screen;
import org.zjx.state.StateManager;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;

public class ClickNpcCommand extends Command {

    public enum ClickNpcType {
        NPC,
        ATTACK,
        GIVE;
    }

    private final String npcName;
    private final String screenName;
    private final ClickNpcType clickNpcType;

    public ClickNpcCommand(IWindow targetWindow, StateManager stateManager, String npcName, String screenName, ClickNpcType clickNpcType) {
        super(targetWindow, false, stateManager);
        this.npcName = npcName;
        this.screenName = screenName;
        this.clickNpcType = clickNpcType;
    }

    @Override
    protected void doExecute() throws Exception {
        Screen screen = CommandFactory.getScreenByName(targetWindow, screenName);
        Npc npc = CommandFactory.getNpc(targetWindow, npcName, screen.getId());

        if (npc == null) throw new IllegalArgumentException("npc is null");
        List<Point> points = JSON.parseArray(npc.getAddresses(), Point.class);
        if (points == null) throw new IllegalArgumentException("point is null");
        Point point = points.get(0);
        Address targetAddress = new Address(screenName, point.x, point.y);

        Address currentAddress = CommandFactory.getAddress(targetWindow, CommandFactory.screenshot(targetWindow));
//        if (!targetAddress.near(currentAddress, 5)) {
//            //TODO 重新寻路
//            throw new IllegalArgumentException("target address near to current address");
//        }

        GameMoveLocationCommand gameMoveLocationCommand = new GameMoveLocationCommand(targetWindow, stateManager, screen, point.x, point.y);
        gameMoveLocationCommand.execute();

        if (gameMoveLocationCommand.getGameMouse() != null && gameMoveLocationCommand.getGameMouse().type.equals(GameMouse.Type.NPC)) {
            if (clickNpcType.equals(ClickNpcType.ATTACK)) {
                KeyCombinationCommand keyCombinationCommand = new KeyCombinationCommand(targetWindow, stateManager, KeyEvent.VK_ALT, KeyEvent.VK_A);
                keyCombinationCommand.execute();
            } else if (clickNpcType.equals(ClickNpcType.GIVE)) {
                KeyCombinationCommand keyCombinationCommand = new KeyCombinationCommand(targetWindow, stateManager, KeyEvent.VK_ALT, KeyEvent.VK_G);
                keyCombinationCommand.execute();
                CommandFactory.waitDirectly(targetWindow, 300);
                ClickCommand clickCommand = new ClickCommand(targetWindow, 1, 1);
                clickCommand.execute();
            } else {
                ClickCommand clickCommand = new ClickCommand(targetWindow, 1, 1);
                clickCommand.execute();
            }
        } else {
            //TODO 隐藏角色/摊位
        }
        CommandFactory.waitDirectly(targetWindow, 1000);
    }
}
